package com.letao.controllers;

import com.letao.Dao.CreateFoodDao;
import com.letao.models.CreateFood;
import com.letao.models.ReturnMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.lang.Object;

/**
 * Created by youweirong on 2017/3/1.
 */
@RestController
public class IndexController {
	@RequestMapping("/")
	public String indexShow(){
		return "Hello Letao!";
	}

	@Autowired
	private CreateFoodDao createFoodDao;

	@RequestMapping("/create")
	public Object create(){
		CreateFood createFood = new CreateFood();
		createFood.setName("Tomato");
		return createFoodDao.save(createFood);
	}

	@RequestMapping("/showall")
	public Object showAll(){
		return createFoodDao.findAll();
	}
/*
	@RequestMapping(value = "/verify")
	public Object verify(String stringId){
		return stringId;
	}
	@RequestMapping(value = "/verify")
	public Object verify(ReturnMessage returnMessage){
		return returnMessage;
	}
	@RequestMapping(value = "/verify")
	public Object verify(@RequestParam(value = "id",defaultValue = "-1") String stringId){
		return stringId;
	}
*/
	@RequestMapping(value = "/verify")
	public Object verify(@RequestParam(value = "id",defaultValue = "-1")String stringId){
		long id=-1;
		try{
			id =Integer.parseInt(stringId);
		}catch(Exception e){
			id =-1;
		}
		CreateFood createFood=createFoodDao.findOne(id);
		ReturnMessage returnMessage = new ReturnMessage();
		if(id<0){
			returnMessage.setId(2);
			returnMessage.setMessage("ID Illgal");
		}else if(createFood == null){
			returnMessage.setId(1);
			returnMessage.setMessage("ID Not Found");
		}else{
			returnMessage.setId(0);
			returnMessage.setMessage("Today we eat "+createFood.getName());
		}
		return returnMessage;
	}

}
